// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.commands;

import org.dengues.core.process.IGefNode;
import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.models.CompConnection;
import org.dengues.designer.ui.process.models.CompNode;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class CompConnectionReconnectionCommand extends AbstractGEFCommand {

    private final CompConnection conn;

    private IGefNode newSource;

    private IGefNode oldSource;

    private IGefNode newTarget;

    private IGefNode oldTarget;

    /**
     * Qiang.Zhang.Adolf@gmail.com CompConnectionReconnectionCommand constructor comment.
     * 
     * @param conn
     */
    public CompConnectionReconnectionCommand(CompConnection conn) {
        this.conn = conn;
        this.oldSource = conn.getSource();
        this.oldTarget = conn.getTarget();

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#execute()
     */
    @Override
    public void execute() {
        if (newSource != null) {
            conn.reconnect(newSource, oldTarget);
        } else if (newTarget != null) {
            conn.reconnect(oldSource, newTarget);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#undo()
     */
    @Override
    public void undo() {
        if (newSource != null) {
            conn.reconnect(oldSource, oldTarget);
        } else if (newTarget != null) {
            conn.reconnect(oldSource, oldTarget);
        }
    }

    /**
     * Getter for newSource.
     * 
     * @return the newSource
     */
    public IGefNode getNewSource() {
        return this.newSource;
    }

    /**
     * Sets the newSource.
     * 
     * @param newSource the newSource to set
     */
    public void setNewSource(CompNode newSource) {
        setLabel(Messages.getString("CompConnectionReconnectionCommand.source")); //$NON-NLS-1$
        this.newTarget = null;
        this.newSource = newSource;
    }

    /**
     * Getter for oldSource.
     * 
     * @return the oldSource
     */
    public IGefNode getOldSource() {
        return this.oldSource;
    }

    /**
     * Sets the oldSource.
     * 
     * @param oldSource the oldSource to set
     */
    public void setOldSource(CompNode oldSource) {
        this.oldSource = oldSource;
    }

    /**
     * Getter for newTarget.
     * 
     * @return the newTarget
     */
    public IGefNode getNewTarget() {
        return this.newTarget;
    }

    /**
     * Sets the newTarget.
     * 
     * @param newTarget the newTarget to set
     */
    public void setNewTarget(CompNode newTarget) {
        setLabel(Messages.getString("CompConnectionReconnectionCommand.target")); //$NON-NLS-1$
        this.newSource = null;
        this.newTarget = newTarget;
    }

    /**
     * Getter for oldTarget.
     * 
     * @return the oldTarget
     */
    public IGefNode getOldTarget() {
        return this.oldTarget;
    }

    /**
     * Sets the oldTarget.
     * 
     * @param oldTarget the oldTarget to set
     */
    public void setOldTarget(CompNode oldTarget) {
        this.oldTarget = oldTarget;
    }

}
